package com.lzp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzp.entity.Schools;
import com.lzp.entity.Teacher;
import com.lzp.mapper.TeacherMapper;
import com.lzp.service.ISchoolsService;
import com.lzp.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzp.util.PageResult;
import com.lzp.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzp
 * @since 2025-01-13
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    private ISchoolsService schoolsService;

    @Override
    public PageResult<Teacher> findByPage(Integer page, Integer pageSize) {
        //分页
        Page<Teacher> page1=this.page(new Page<>(page,pageSize));
        //封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    //条件带分页查询
    @Override
    public PageResult<Teacher> search(Integer page, Integer pageSize, TeacherVO vo) {
        //进行分页条件查询-
        //1-1.封装查询条件
        QueryWrapper<Teacher> wrapper =new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getTname()),"tname",vo.getTname())
        .like(StrUtil.isNotBlank(vo.getAddr()),"addr",vo.getAddr())
        .eq(vo.getSid()!=null && vo.getSid()!=0,"sid",vo.getSid());
        //1-2.开始分页查询
        Page<Teacher> page1 = this.page(new Page<>(page, pageSize), wrapper);
        page1.getRecords().forEach(st->{
            Schools schools = schoolsService.getById(st.getSid());
            st.setSname(schools.getSname());//拿到分院名字
        });
        //2.封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    //添加老师
    @Override
    public boolean add(Teacher teacher){
        return this.save(teacher);
    }

}



























